package sample.blogger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



public class DateFactory {

	public static final String TAG = "DateFactory";

	public static final String SHORT_FORMAT_STRING = "yyyy-MM-dd";

	public static final SimpleDateFormat SDF = new SimpleDateFormat(
			SHORT_FORMAT_STRING);

	public static final SimpleDateFormat SDF_LONG = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm");

	public String getToday() {
		Calendar c = this.getCalendar();
		return SDF.format(c.getTime());
	}

	public Calendar getCalendar() {
		return Calendar.getInstance();
	}

	public String getToday(int dayOffset) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, dayOffset);

		return SDF.format(c.getTime());
	}

	public static Date getTodaysDate() {
		Calendar c = Calendar.getInstance();
		return c.getTime();
	}

	public boolean isBeforeToday(String in) {
		Calendar c = Calendar.getInstance();

		try {
			Date d = SDF.parse(in);
			c.setTime(d);
			return this.getCalendar().before(c);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;

	}

	public static Date stringToDate(String in) throws Exception {
		return SDF.parse(in);
	}

	public static Date advance(Date in, int daysForward) {
		Calendar c = Calendar.getInstance();
		c.setTime(in);
		c.add(Calendar.DAY_OF_YEAR, daysForward);
		return c.getTime();
	}

	public String getNowString() {
		Calendar c = getCalendar();
		Date d = c.getTime();
		return SDF_LONG.format(d);
	}

	public static int dateDiff(Date begin, Date end) {

		Calendar startDate = Calendar.getInstance();
		startDate.setTime(begin);

		Calendar endDate = Calendar.getInstance();
		endDate.setTime(end);

		Calendar date = (Calendar) startDate.clone();
		int daysBetween = 0;
		while (date.before(endDate)) {
			date.add(Calendar.DAY_OF_MONTH, 1);
			daysBetween++;
		}
		return daysBetween;
	}

	public static int daysAgo(Date begin) {

		return DateFactory.dateDiff(begin, DateFactory.getTodaysDate());

	}

}
